buildscript {
    dependencies {
        classpath project(':app')
    }
}

plugins {
    id 'com.android.application'
}

def getGitHubCommit = {
    try {
        def hashOutput = new ByteArrayOutputStream()
        def changeOutput = new ByteArrayOutputStream()
        def gitVersionName
        exec {
            commandLine 'git', 'rev-list', '--max-count=1', 'HEAD'
            standardOutput = hashOutput
        }
        exec {
            commandLine 'git', 'diff-index', '--shortstat', 'HEAD'
            standardOutput = changeOutput
        }
        gitVersionName = hashOutput.toString().trim().substring(0, 7);
        if (!changeOutput.toString().trim().empty) {
            def pattern = Pattern.compile("\\d+");
            def matcher = pattern.matcher(changeOutput.toString().trim())
            if (matcher.find()) {
                gitVersionName += "-" + matcher.group()
            }
        }
        return gitVersionName
    } catch (ignored) {
        return "UNKNOWN";
    }
}

def getTDAppToken = {
    /*
         HOW TO STORE TELEGRAM API KEY SAFELY?

         1.  Create file named 'tdlib.prop' in '.signing' directory.
         2.  Generate Base58 hash according to the format of the original string:
             '<api_id>.<api_hash>'
             (without quotation marks)
         3.  Store 'tdlib.prop' in a place inaccessible to third parties.
    */
    Properties tdlib_props = new Properties();
    if (file('.signing/tdlib.prop').exists()) {
        tdlib_props.load(new FileInputStream(file(".signing/tdlib.prop")))
        return "${tdlib_props['APP_TOKEN']}"
    } else {
        return "UNKNOWN.UNKNOWN"
    }
}

android {
    namespace 'dev.tinelix.jabwave'
    compileSdk 34

    defaultConfig {
        applicationId "dev.tinelix.jabwave"
        minSdk 19
        targetSdk 34
        versionCode 1
        versionName "0.0.1-prealpha"
        multiDexEnabled true
        buildConfigField "String", "GIT_COMMIT", "\"${getGitHubCommit()}\""
        configurations.all {
            resolutionStrategy {
                force 'androidx.core:core:1.12.0'
            }
        }
        buildConfigField "String", "SOURCE_CODE", "\"https://github.com/tinelix/jabwave\""
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        debug {

        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_16
        targetCompatibility JavaVersion.VERSION_16
    }
    buildFeatures {
        viewBinding true
    }
    flavorDimensions 'version'
    productFlavors {
        tdlib {
            dimension 'version'
            versionNameSuffix '.tdlib'
            applicationId 'dev.tinelix.jabwave.td'
            buildConfigField "String", "BUILD_VARIANT", "\"tdlib\""
            buildConfigField "String", "TDLIB_APP_TOKEN", "\"${getTDAppToken()}\""
        }
        smack {
            dimension 'version'
            versionNameSuffix '.smack'
            applicationId 'dev.tinelix.jabwave.sm'
            buildConfigField "String", "BUILD_VARIANT", "\"smack\""
        }
        combo {
            dimension 'version'
            buildConfigField "String", "TDLIB_APP_TOKEN", "\"${getTDAppToken()}\""
            buildConfigField "String", "BUILD_VARIANT", "\"combo\""
        }
    }

    sourceSets {
        tdlib {
            java.srcDirs = ['src/tdlib/java']
            res.srcDirs = ['src/tdlib/res']
            manifest.srcFile 'src/tdlib/AndroidManifest.xml'
        }
        smack {
            java.srcDirs = ['src/smack/java']
            res.srcDirs = ['src/smack/res']
            manifest.srcFile 'src/smack/AndroidManifest.xml'
        }
        combo {
            java.srcDirs = ['src/combo/java']
            res.srcDirs = ['src/combo/res']
            manifest.srcFile 'src/combo/AndroidManifest.xml'
        }
    }
}

dependencies {
    // Combo build-specified dependencies
    comboImplementation project(path: ':tdlib')
    comboImplementation project(path: ':tdlib-wrap')
    comboImplementation 'org.jxmpp:jxmpp-jid:1.0.3'
    comboImplementation "org.igniterealtime.smack:smack-tcp:4.4.6"
    comboImplementation "org.igniterealtime.smack:smack-im:4.4.6"

    // Smack build-specified dependencies
    smackImplementation 'org.jxmpp:jxmpp-jid:1.0.3'
    smackImplementation "org.igniterealtime.smack:smack-tcp:4.4.4"
    smackImplementation "org.igniterealtime.smack:smack-im:4.4.4"

    // TDLib build-specified dependencies
    tdlibImplementation project(path: ':tdlib')
    tdlibImplementation project(path: ':tdlib-wrap')

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.11.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment:2.7.7'
    implementation 'androidx.navigation:navigation-ui:2.7.7'
    implementation "androidx.preference:preference-ktx:1.2.1"

    implementation ("org.igniterealtime.smack:smack-android-extensions:4.4.0") {
        exclude module: 'xpp3'
    }
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    implementation project(path: ':api')

    annotationProcessor ('com.github.bumptech.glide:compiler:4.12.0')
    implementation 'com.github.luizgrp:SectionedRecyclerViewAdapter:v3.2.0'
    implementation "androidx.emoji2:emoji2:1.4.0"
    implementation 'com.github.mediapark-pk:Base58-android:0.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'org.apmem.tools:layouts:1.10@aar'
    implementation 'com.takisoft.preferencex:preferencex:1.1.0'
}